<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") {
header('Location: index.php');
die();
}


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
if (!$db_rdata->isConnected()) {	
	die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
}	

include("header.php");
include("menu.php");

if ($kreditas == "1") {

$showTable = 0;
if (isset($_REQUEST["act"])){
	if ($_REQUEST["act"] == "change" && isset($_REQUEST["id"]) && isset($_REQUEST["card"])) {
		$vchange_query = str_replace("<id>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["id"])), $vchange_query);
		$vchange_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["card"])), $vchange_query);
		$db_rdata->execute($vchange_query);

		$showTable = 1;
	} else if ($_REQUEST["act"] == "del" && isset($_REQUEST["id"])){
		$vdel_query = str_replace("<id>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["id"])), $vdel_query);
		$db_rdata->execute($vdel_query);	
		$showTable = 1;
	}
}


if (isset($_REQUEST['znr'])){
	$_SESSION['znr'] = $_REQUEST['znr'];
} else {
	if (!isset($_SESSION['znr'])) {
		$_SESSION['znr'] = "";
	} 
}

if (isset($_REQUEST['aid'])){
	$_SESSION['aid'] = $_REQUEST['aid'];
} else {
	if (!isset($_SESSION['aid'])) {
		$_SESSION['aid'] = "";
	} 
}

if (isset($_REQUEST['kvitonr'])){
	$_SESSION['kvitonr'] = $_REQUEST['kvitonr'];
} else {
	if (!isset($_SESSION['kvitonr'])) {
		$_SESSION['kvitonr'] = "";
	} 
}

if (isset($_REQUEST['from'])){
	$_SESSION['ffrom'] = $_REQUEST['from'];
} else {
	if (!isset($_SESSION['ffrom'])) {
		$_SESSION['ffrom'] = date("Y.m.d");
	} 
}

if (isset($_REQUEST['to'])){
	$_SESSION['fto'] = $_REQUEST['to'];
} else {
	if (!isset($_SESSION['fto'])) {
		$_SESSION['fto'] = date("Y.m.d");
	} 
}

if (isset($_REQUEST['poz'])){
	$_SESSION['fpoz'] = $_REQUEST['poz'];
} else {
	if (!isset($_SESSION['fpoz'])) {
		$_SESSION['fpoz'] = "0";
	} 
}

?>
<!-- IE specific code: -->
<!--[if lte IE 9]> 
<style type="text/css"> 
#aep_ovrl { 
position: absolute; 
filter:alpha(opacity=70); 
top: expression(eval(document.body.scrollTop)); 
width: expression(eval(document.body.clientWidth)); 
} 
#aep_ww {  
position: absolute;  
top: expression(eval(document.body.scrollTop));  
} 
</style> 
<![endif]-->
<!-- ae_prompt HTML code 
<div id="aep_ovrl" style="display: none;"></div>
<div id="aep_ww" style="display: none;">
<div id="aep_win"><div id="aep_t"></div>
<div id="aep_w"><div id="aep_center"><span id="aep_prompt"></span>
<br /><br />Kortelės nr: <input type="text" id="aep_text" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);">
<table width="100%" cellpadding="0" cellspacing="0">
<tr><th>Kiekis</th><th>Suma</th></tr>
</table>
<br><div><input class="but bgfilter" type="button" id="aep_ok" onclick="ae_clk(1);" value="Gerai">
<input class="but bgfilter" type="button" id="aep_cancel" onclick="ae_clk(0);" value="Atšaukti">
</div></div>
</div>
</div>
</div>-->
<!-- ae_prompt HTML code -->

<?php

echo "<table class='innercont' cellspacing='0' cellpadding='0'><tr><td colspan='2'>&nbsp;</td></tr>";
echo "<tr><td class='leftcol' style='text-align:right;'>";

//if (isset($_REQUEST["poz"]) && $_REQUEST["poz"]=="2") {
?>
<table cellspacing="0" cellpadding="5" class="params" style="">
<tr>
<td style="text-align:center;"><a id="newButton" href="voucher.php" class="card3">Naujas kvitas</a></td>
</tr>
</table>
<br>

<script type="text/javascript">
	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
</script>
  <script>
  $(function() {
    $( ".dt" ).datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true,
	  dateFormat: "yy.mm.dd",
	  regional: "lt"
    });
  });
  </script>

	<form method="get"><table cellspacing='0' cellpadding='5' class='params'>
	<tr><th>Znr:</th></tr>
	<tr><td style='text-align:left;'><input  class="inp" type='text' name="znr" value="<?php echo htmlspecialchars($_SESSION['znr']);?>"></td></tr>
	<tr><th>Aparato Id:</th></tr>
	<tr><td style='text-align:left;'><input  class="inp" type='text' name="aid" value="<?php echo htmlspecialchars($_SESSION['aid']);?>"></td></tr>
	<tr><th>Kvito nr2:</th></tr>
	<tr><td style='text-align:left;'><input  class="inp" type='text' name="kvitonr" value="<?php echo htmlspecialchars($_SESSION['kvitonr']);?>"></td></tr>
<?php
		echo "<tr><th>Pasirinkite periodą:</th></tr>";
		echo "<tr><td>Nuo&nbsp;&nbsp;&nbsp;<input class=\"dt\" type='text' name=\"from\" value=\"" . $_SESSION['ffrom'] ."\"></td></tr>";
		echo "<tr><td>Iki&nbsp;&nbsp;&nbsp;<input class=\"dt\" type='text' name=\"to\" value=\"" . $_SESSION['fto'] ."\"></td></tr>";
	echo "<tr><th></th></tr>";
	echo "<tr><td style='text-align:left;'><div><select class='select' name=\"poz\">";
	echo "<option value='0' ";  if ($_SESSION['fpoz']=="0") {echo 'selected="selected"';}echo ">Visi</option>";
	echo "<option value='2' ";  if ($_SESSION['fpoz']=="2") {echo 'selected="selected"';}echo ">Koreguoti</option>";
	echo "<option value='1' ";  if ($_SESSION['fpoz']=="1") {echo 'selected="selected"';}echo ">Nauji</option>";
	echo "</select></div></td></tr>";		
		
		
		
?>
	<tr><td><table width="100"><tr><td><button class='but bgfilter' style="width:40px;" onclick="window.location='overview.php';return false;">Atgal</button></td><td align="right"><input class='but bgfilter' type = "submit" name="sub" value = "Rodyti"></td><tr></table></td></tr>
	</table>
	</form>
	
<?php

echo "</td>";
echo "<td class='rightcol'>";
?>
<script>
var id = null;

function modVouncher(iid){
    id = iid;
	window.location='voucher.php?id='+iid;
}


function delVoucher(id){
	var answer = confirm("Ar ištrinti kvitą?");
	if (answer){
		window.location='vouchers.php?act=del&id='+id ;
	}

}



</script>
<?php
if ((isset($_REQUEST["sub"]) && $_REQUEST["sub"] == "Rodyti") || $showTable == 1) {
    if ($_SESSION['fpoz'] == "0") {
		$vouncher_query = str_replace("<import>", "1=1 ", $vouncher_query);
	} else {
		$vouncher_query = str_replace("<import>", " import  = " . addslashes_mssql($_SESSION['fpoz']), $vouncher_query);
	}

	if (trim($_SESSION['znr']) != "") {
		$vouncher_query .= " AND Znr='".addslashes_mssql(trim($_SESSION['znr']))."'";
	}
	if (trim($_SESSION['aid']) != "") {
		$vouncher_query .= " AND AparatoID='".addslashes_mssql(trim($_SESSION['aid']))."'";
	}
	if (trim($_SESSION['kvitonr']) != "") {
		$vouncher_query .= " AND Kvitonr2='".addslashes_mssql(trim($_SESSION['kvitonr']))."'";
	}	
	if (trim($_SESSION['ffrom']) != "") {
		$vouncher_query .= " AND KvitoGalva.data >= cast('".$_SESSION['ffrom']." 00:00:00' as datetime) ";
	}
	if (trim($_SESSION['fto']) != "") {
		$vouncher_query .= " AND KvitoGalva.data <= cast('".$_SESSION['fto']." 23:59:59' as datetime) ";
	}
	//echo $vouncher_query;
	
	$pageSize = 6000;
	if (!isset($_REQUEST["p"]) || !is_numeric($_REQUEST["p"])) {
		$page = 1;
	} else {
		$page = $_REQUEST["p"];
	}
	$start1 = ($pageSize * ($page-1))+1;
	$end1 = $pageSize * $page;
	
	$vq = "SELECT * FROM (" . $vouncher_query .") as RowConstrainedResult WHERE RowNum >= $start1 AND RowNum < $end1 ORDER By RowNum";
	$ns = $db_rdata->returnList($vq);
	echo '<table class="innercont2" cellspacing="0" cellpadding="2" style="width:756px;"><tr><th>Deg. Id</th><th>Data</th><th>Apm. rūšis</th><th style="text-align:right;">Suma</th><th>Nuol.</th><th>Mokes<br>čiai</th><th>Kortelės nr.</th><th>Znr</th><th>Kvito nr2.</th><th>Pož.</th><th>Barkodas</th><th>Kiekis</th>';
		

	echo '</tr>';
	$vsuma = 0;
	$vmok = 0;
	//echo count($ns);
	foreach ($ns as $kvitas) {
		$click = "onclick=\"modVouncher('".$kvitas["ID"]."');\" class='click'";
		echo "<tr >";
		echo "<td $click style='text-align:right;'>".$kvitas["AparatoID"]."</td>";
		echo "<td nowrap $click>".$kvitas["data1"]."</td>";
		echo "<td $click>".$kvitas["ApmokejimoRusis"]."</td>";
		echo "<td $click style='text-align:right;'>".sprintf("%01.2f",$kvitas["Suma1"])."</td>";
		echo "<td $click style='text-align:right;'>".sprintf("%01.2f",$kvitas["Nuolaida1"])."</td>";
		echo "<td $click style='text-align:right;'>".sprintf("%01.2f",$kvitas["Mokesciai1"])."</td>";
		echo "<td $click>".$kvitas["KortelesNr"]."</td>";
		echo "<td $click>".$kvitas["Znr"]."</td>";
		echo "<td $click>".$kvitas["KvitoNr2"]."</td>";
		if ($kvitas["import"]=="1") {
			echo "<td>N</td>";
		} else if ($kvitas["import"]=="2") {
			echo "<td>K</td>";
		} else {
			echo "<td></td>";
		}
		echo "<td $click>".$kvitas["Barkodas1"]."</td>";
		echo "<td $click>".$kvitas["Kiekis1"]."</td>";
		if ($kvitas["import"]=="1") {
			echo "<td class='click' onclick=\"delVoucher('".$kvitas["ID"]."');return false;\"><img src='img/delete.jpg' width='11' alt='Trinti' title='Trinti'></td>";
		} else {
			echo "<td></td>";
		}
		echo "</tr>";
		$vsuma += $kvitas["Suma1"];
		$vmok += $kvitas["Mokesciai1"];
	}
	
	echo '<tr><th></th><th></th><th>Viso:</th><th style="text-align:right;">'.sprintf("%01.2f",$vsuma).'</th><th></th><th style="text-align:right;">'.sprintf("%01.2f",$vmok).'</th><th></th><th></th><th></th><th></th><th></th>';
	echo "<th>&nbsp;</th>";

	echo '</tr>';
	
	echo "</table>";
	}
	echo "</td></tr></table>";
}
include("footer.php");
?>